package com.youkeyi.ddy.cloud.application.system.corp.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.corp.SystemCorpSortCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.dto.SystemCorpSortListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.dto.SystemCorpSortListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.param.SystemCorpSortListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.param.SystemCorpSortUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.PublicCorpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.PublicCorpSortCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpSortListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpSortListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpSortCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpSortDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpSortQueryCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * SystemCorpSortController
 *
 * @Author liuxiawang
 * @Date 2024/5/23 10:23 AM
 * @Location shenzhen.china
 */
@Service
public class SystemCorpSortController implements SystemCorpSortCloudService {

    @Autowired
    private PublicCorpSortCloudService publicCorpSortCloudService;
    @Autowired
    private PublicCorpCloudService publicCorpCloudService;

    @Override
    public SystemCorpSortListCloudDto list(SystemCorpSortListCloudParam param) {
        SystemCorpSortListCloudDto dto = new SystemCorpSortListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicCorpSortQueryCloudParam corpSortQueryCloudParam = new PublicCorpSortQueryCloudParam();
        corpSortQueryCloudParam.setCorpPrimaryKeys(param.getCorpPrimaryKeys());
        PublicCorpSortListCloudDto corpSortDtos = publicCorpSortCloudService.query(corpSortQueryCloudParam);
        if (null != corpSortDtos && CollectionUtils.isNotEmpty(corpSortDtos.getRows())) {
            PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
            corpQueryCloudParam.setCorpPrimaryKeys(corpSortDtos.getRows().stream().map(PublicCorpSortListItemCloudDto::getCorpKey).collect(Collectors.toList()));
            PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);
            Map<String, PublicCorpListItemCloudDto> corpMap = new HashMap<>();
            if (null != corpDtos && CollectionUtils.isNotEmpty(corpDtos.getRows())) {
                corpMap.putAll(corpDtos.getRows().stream().collect(Collectors.toMap(PublicCorpListItemCloudDto::getCorpPrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            List<SystemCorpSortListItemCloudDto> items = new ArrayList<>();
            corpSortDtos.getRows().forEach(corpSort -> {
                SystemCorpSortListItemCloudDto item = new SystemCorpSortListItemCloudDto();
                item.setCorpSortPrimaryKey(corpSort.getCorpSortPrimaryKey());
                item.setCorpKey(corpSort.getCorpKey());
                if (corpMap.containsKey(corpSort.getCorpKey())) {
                    PublicCorpListItemCloudDto corp = corpMap.get(corpSort.getCorpKey());
                    item.setCorpName(corp.getName());
                    item.setCorpCategory(corp.getCategory());
                    item.setCorpCategoryName(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_CATEGORY.getName(item.getCorpCategory()));
                    item.setCorpType(corp.getType());
                    item.setCorpTypeName(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_TYPE.getName(item.getCorpType()));
                }
                item.setMappingCorpKey(corpSort.getMappingCorpKey());
                item.setSort(corpSort.getSort());
                item.setRemark(corpSort.getRemark());
                item.setIsDeleted(corpSort.getIsDeleted());
                item.setIsDeletedName(corpSort.getIsDeletedName());
                item.setCreateUserKey(corpSort.getCreateUserKey());
                item.setCreateTime(corpSort.getCreateTime());
                item.setUpdateUserKey(corpSort.getUpdateUserKey());
                item.setUpdateTime(corpSort.getUpdateTime());
                item.setCreateUserName(corpSort.getCreateUserName());
                item.setUpdateUserName(corpSort.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(corpSortDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent update(SystemCorpSortUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || StringUtils.isBlank(param.getCorpKey()) || CollectionUtils.isEmpty(param.getItems())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicCorpSortQueryCloudParam corpSortQueryCloudParam = new PublicCorpSortQueryCloudParam();
        corpSortQueryCloudParam.setCorpPrimaryKeys(Collections.singletonList(param.getCorpKey()));
        PublicCorpSortListCloudDto corpSortDtos = publicCorpSortCloudService.query(corpSortQueryCloudParam);
        if (null != corpSortDtos && CollectionUtils.isNotEmpty(corpSortDtos.getRows())) {
            PublicCorpSortDeleteCloudParam deleteCloudParam = new PublicCorpSortDeleteCloudParam();
            deleteCloudParam.setCorpSortPrimaryKeys(corpSortDtos.getRows().stream().map(PublicCorpSortListItemCloudDto::getCorpSortPrimaryKey).collect(Collectors.toList()));
            deleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            deleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
            publicCorpSortCloudService.delete(deleteCloudParam);
        }

        param.getItems().forEach(item -> {
            PublicCorpSortCreateCloudParam createCloudParam = new PublicCorpSortCreateCloudParam();
            createCloudParam.setCorpKey(param.getCorpKey());
            createCloudParam.setMappingCorpKey(item.getMappingCorpKey());
            createCloudParam.setSort(item.getSort());
            createCloudParam.setRemark(item.getRemark());
            createCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            createCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
            publicCorpSortCloudService.create(createCloudParam);
        });

        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return rc;
    }

}
